<template>
    <div class="tabs-content">
        <!--***********************************************流程定义文件*********************************************************-->
        <!-- 头部查询区域 -->
        <el-form
          :inline="true"
          :model="dataForm2"
          style="display: flex; justify-content: space-between; align: center; margin: 0px 20px">
          <div style="display:inline-block">
            <el-form-item>
              <el-input v-model="dataForm2.id" id="minput" placeholder="请输入流程编号"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="searchDataList()">查 询</el-button>
            </el-form-item>
            <el-form-item>
              <el-button @click="reset()">重 置</el-button>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" class="defineC" @click="cimsInputClick">定义流程</el-button>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" class="defineC" @click="cimsInputClick2">上传流程</el-button>
            </el-form-item>
          </div>
          </el-form>
          <!-- 内容区域 -->
          <el-table
            ref="NowDefineDataList"
            border
            :data="NowDefineDataList"
            height="73.5%"
            style="width: 97.5%"
            :header-cell-style="{height: '7vh'}"
            :cell-style="cellStyle"
          >
            <el-table-column
              width="55"
              type="index"
              :index="getIndex"
              label="序号"
              header-align="center"
              align="center"
            >
            </el-table-column>
            <el-table-column
              label="流程编号"
              prop="id"
              header-align="center"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="title"
              label="主题"
              header-align="center"
              align="center">
            </el-table-column>
            <el-table-column
              prop="workID"
              label="上传人工号"
              header-align="center"
              align="center">
            </el-table-column>
            <el-table-column
              prop="description"
              label="主要描述"
              header-align="center"
              align="center">
            </el-table-column>
            <el-table-column
              prop="uploadDate"
              label="上传时间"
              header-align="center"
              align="center">
            </el-table-column>
            <el-table-column
              width="240"
              header-align="center"
              align="center"
              label="操作">
              <template slot-scope="scope">
                <el-button type="primary" @click="handleLook(scope.row)" style="width:3vw;height: 4vh;">查 看</el-button>
                <el-button size="small" type="success" style="width:3vw;height: 4vh;" @click="handleDeploy(scope.row)" :disabled="scope.row.deploy ==1?true:false">部&nbsp;署</el-button>
                <el-button size="small" type="danger" style="width:3vw;height: 4vh;" @click="handleDelete2(scope.row)">删 除</el-button>
                <!-- <el-link type="primary" @click="handleLook(scope.row)">查看</el-link>
                <el-link type="success" @click="handleDeploy(scope.row)" :disabled="scope.row.deploy ==1?true:false">部署</el-link>
                <el-link type="danger" @click="handleDelete2(scope.row)">删除</el-link> -->
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页器 -->
          <div class="block">
            <el-pagination
              align="right"
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-size="pageSize"
              layout="total, prev, pager, next, jumper"
              :total="total">
            </el-pagination>
          </div>
          <!-- 弹窗，新增或修改 -->
          <el-dialog
                :title="textMap[dialogStatus]"
                :close-on-click-modal=true
                :visible.sync="dialogFormVisible"
                >
                <el-form
                class="Addform"
                :model="dataForm"
                ref="dataForm"
                @keyup.enter.native="dataFormSubmit()"
                label-width="110px"
                label-position="left"
                style="width: 400px; margin-left: 80px">
                  <div class="right1">
                    <el-row :gutter="15">
                        <el-form-item label="主题" style="width: 370px;" prop="dName" >
                          <el-input v-model="dataForm.title" ></el-input>
                        </el-form-item>
                        <el-form-item label="上传人工号" style="width: 370px;" prop="dClass">
                          <el-input disabled v-model="dataForm.workID" ></el-input>
                        </el-form-item>
                        <el-form-item label="主要描述" style="width: 370px;" prop="dPrice">
                          <el-input v-model="dataForm.description" ></el-input>
                        </el-form-item>
                        <el-form-item label="上传时间" style="width: 370px;" prop="dAmount">
                          <el-input v-model="dataForm.uploadDate" ></el-input>
                        </el-form-item>
                    </el-row>
                  </div>
                </el-form>
                <span slot="footer" class="dialog-footer">
                  <el-button type="primary" @click="dialogStatus === 'create' ? createData() : lookData()">确定</el-button>
                  <el-button type="primary" @click="dialogFormVisible = false">取消</el-button>
                </span>
          </el-dialog>
          <!--弹窗，上传流程文件-->
          <el-dialog
            :title="textMap[dialogStatus]"
            :close-on-click-modal=true
            :visible.sync="dialogFormVisible2"
              @open="fixUserId">
                <el-form
                :rules="defineRules"
                :model="dataFormUpload"
                class="Addform"
                ref="dataForm"
                @keyup.enter.native="dataFormSubmit()"
                label-width="110px"
                label-position="left"
                style="width: 400px; margin-left: 80px">
                  <el-row :gutter="15">
                  <el-form-item label="主题" style="width: 370px;" prop="title" auto-complete="on">
                    <el-input v-model="dataFormUpload.title" ></el-input>
                  </el-form-item>
                  <el-form-item label="上传人工号" style="width: 370px;" prop="workID" auto-complete="on">
                    <el-input disabled v-model="dataFormUpload.workID" ></el-input>
                  </el-form-item>
                  <el-form-item label="主要描述" style="width: 370px;" prop="description" auto-complete="on">
                    <el-input v-model="dataFormUpload.description" ></el-input>
                  </el-form-item>
                  <el-form-item prop="file">
                    <el-upload
                    ref="upload"
                    class="upload-demo"
                    action="/process/processFile"
                    :data="dataFormUpload"
                    :on-success="upFile"
                    :on-change="getfile"
                    :auto-upload="false"
                    :file-list="fileList"
                    :headers="header">
                      <el-button  slot="trigger" size="small" type="primary">选择文件</el-button>
                      <el-button style="margin-left:5px" size="small" @click="submitForm('dataForm')">保存</el-button>
                      <el-button size="small" @click="resetForm('dataForm')">重置</el-button>
                    </el-upload>
                  </el-form-item>
                </el-row>
                </el-form>
                <!-- <span slot="footer" class="dialog-footer">
                  <el-button type="primary" @click="submitForm">确定</el-button>
                  <el-button type="primary" @click="dialogFormVisible2 = false">取消</el-button>
                </span> -->
          </el-dialog>
    </div>
</template>

<script>
import axios from 'axios'
export default {
  name: 'NowDefine',
  data() {
    return {
      activeName: 'nowdefine',
      // tableData: [], // 表格中的数据
      // tableData2: [],
      options: [{
        value: '设备编号',
        label: '设备编号'
      }, {
        value: '设备名称',
        label: '设备名称'
      }, {
        value: '设备类别',
        label: '设备类别'
      }],
      value: '',
      // *******翻页***********
      NowDefineDataList: [], // 流程定义文件数据
      currentPage: 1, // 当前页码
      pageSize: 5, // 每页选定的显示条数
      total: 0,
      // **********************
      dataForm2: {}, // 想要查询的设备数据
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      dialogFormVisible: false, // 流程定义文件的弹窗可见性
      dialogFormVisible2: false, // 已部署的流程的弹窗可见性
      dataForm: {
        title: '',
        description: '',
        file: ''
      },
      postId: 0, // 存id
      flag: 0,
      dataFormUpload: {
        title: '',
        workID: '',
        description: ''
      },
      fileList: [], // 上传的文件列表
      fileflag: 0,
      authentication: '', // 存token
      user: {}, // 当前登录的用户信息
      defineRules: {
        title: [
          { required: true, message: '主题不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ]
        // file: [
        //   { required: true, message: '请上传文件', trigger: 'change' }
        // ]
      }
      // disabled: true
    }
  },
  // 生命周期
  mounted(activeName) {
    this.getDataList()
    this.authentication = sessionStorage.getItem('tokenStr')
    this.user = JSON.parse(window.sessionStorage.getItem('user'))
    // this.getData()
    // this.getDataList2()
  },
  created() {
    this.user = JSON.parse(window.sessionStorage.getItem('user'))
  },
  computed: {
    header() {
      return {
        authentication: this.authentication
      }
    }
  },
  watch: {
    tableData(newValue, oldValue) {
      this.currentData = newValue.slice((this.currentPage - 1) * this.pageSize, this.currentPage * this.pageSize)
    },
    CurrentPage(newValue, oldValue) {
      this.currentPage = this.tableData.slice((newValue - 1) * this.pageSize, newValue * this.pageSize)
    }
  },
  methods: {
    reset() {
      this.getDataList()
      document.getElementById('minput').value = ''
    },
    renderTime(date) {
      var dateee = new Date(date).toJSON()
      return new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
    },
    fixUserId() {
      const info = JSON.parse(sessionStorage.getItem('user'))
      // console.log('获取userName')
      // console.log(info)
      this.dataFormUpload.workID = info.workId
    },
    // getData() {
    //   const loading = this.$loading({
    //     lock: true, // lock的修改符--默认是false
    //     text: 'Loading', // 显示在加载图标下方的加载文案
    //     spinner: 'el-icon-loading', // 自定义加载图标类名
    //     background: 'rgba(0, 0, 0, 0.7)', // 遮罩层颜色
    //     target: document.querySelector('#table')// loadin覆盖的dom元素节点
    //   })
    //   // 成功回调函数停止加载
    //   loading.close()
    // },
    // // 选择每页行数
    // handleSizeChange(val) {
    //   this.pageSize = val
    //   this.currentPage = 1
    // },
    // // 选择页码
    // handleCurrentChange(val) {
    //   this.currentPage = val
    // },
    // 表格行高
    cellStyle() {
      return {
        'font-size': '14px',
        height: '45px',
        padding: '0'
      }
    },
    // 表头行高
    headerStyle() {
      return {
        height: '55px'
      }
    },
    // ****************翻页*************
    // 改变当前页码
    handleCurrentChange(val) {
      this.currentPage = val
      this.getDataList()
    },
    // 序号
    getIndex(index) {
      return index + (this.currentPage - 1) * this.pageSize + 1
    },
    // // 选择的复选框的数量
    // handleSelectionChange(val) {
    //   this.multipleSelection = val
    // },
    //* *********************************************************流程定义文件********************************************************* */
    // 定义流程按钮
    cimsInputClick() {
      // window.location.href = 'src/views/DocumentCirculation/Design2.vue'
      // window.location.href = 'D:/gitee仓库/soft-project（公文流转1）/src/views/DocumentCirculation/Design2.vue'
      this.$router.push('/process/design')
    },
    // 上传流程文件按钮
    cimsInputClick2() {
      this.dialogFormVisible2 = true
    },
    // 上传流程文件相关方法
    upFile(res, file) {
      console.log(res)
      if (res.msg === '流程定义文件上传成功') {
        this.$message.success(res.msg)
      } else {
        this.$message.error(res.msg)
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.fileflag === 0) {
            this.$message.error('请上传文件')
          } else {
            // alert('submit!')
            this.$refs.upload.submit()
            this.dialogFormVisible2 = false
            // this.$message.success('上传成功')
            this.getDataList()
            this.$forceUpdate()
            this.fileflag = 0
            this.dataFormUpload.title = ''
            this.dataFormUpload.description = ''
            this.dataFormUpload.workID = ''
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
      // this.$refs.upload.submit()
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    returnValue() {
      return {
        token: sessionStorage.getItem('tokenStr')
      }
    },
    // 分片上传
    uploadBySplit(file, identifier, i) { // 文件上传
      const that = this
      // 如果取消上传直接初始化为最初状态
      if (this.fileCancelUpload) {
        this.fileCancelUpload = false
        this.filePercentage = 0
        this.fileList = []
        this.fileButtonDisabled = false
      }
      const chunkSize = 1024 * 1024 * 1 // 分片大小1M
      const { size } = file // 总大小
      const totalChunks = Math.ceil(size / chunkSize)
      // 分片停止条件
      if (i === totalChunks) {
        this.uploadFileDialogVisible = false
        this.$message({ message: '上传成功', type: 'success' })
        return
      }
      // 计算每一篇的起始位置和结束位置
      const start = i * chunkSize
      const end = Math.min(size, start + chunkSize)
      const fileData = file.slice(start, end)
    },
    // 获取文件判断是否上传
    getfile(event, file, fileList) {
      console.log(file)
      if (file != null) {
        this.fileflag = 1
      }
    },

    // 获取“流程定义文件”选定页码的数据
    getDataList() {
      this.getRequest('/process/processFile/' + this.currentPage + '/' + this.pageSize).then((resp) => {
        if (resp) {
          console.log('-------------------------------------------')
          console.log(resp)
          this.total = resp.total
          this.NowDefineDataList = resp.list
          console.log(resp.total)
        } else {
          console.log('失败')
        }
      })
    },
    // 查询
    searchDataList(data) {
      this.dataLoading = true
      this.getRequest('process/processFile/' + this.dataForm2.id).then((resp) => {
        if (resp) {
          // console.log('-------------------------------------------')
          console.log(resp)
          // this.tableData = resp.data
          this.NowDefineDataList = resp.data
        } else {
          console.log('查询失败')
        }
      })
      this.dataLoading = false
      // this.getDataList()
    },
    // 查看
    handleLook(data) {
      this.dialogFormVisible = true
      this.dialogStatus = 'look'
      this.postId = data.id
      this.dataForm = data
    },
    lookData() {
      this.putRequest('process/processFile', {
        title: this.dataForm.title,
        workID: this.dataForm.workID,
        description: this.dataForm.description,
        uploadDate: this.dataForm.uploadDate
      }).then((resp) => {
        if (resp) {
          console.log(resp)
          this.$message.success(resp.msg)
          this.getDataList()
        } else {
          console.log('更新失败')
        }
      })
      this.dialogFormVisible = false
    },
    // 部署
    handleDeploy(data) {
      console.log(this.user.name)
      console.log(2)
      console.log(this.user.name)

      // axios.get(`/process/deploy/${data.id}/${this.user.name}`, {
      //   params: {
      //     id: data.id,
      //     username: this.user.name
      //   },
      //   headers: {
      //     token: tokenStr
      //   }

      this.getRequest(`/process/deploy/${data.id}/${this.user.name}`).then((resp) => {
        if (resp) {
          console.log(resp)
          this.$message.success(resp.msg)
          data.deploy = 0
          this.getDataList()
          this.getDataList2()
        } else {
          console.log('部署失败')
        }
      })
      this.dialogFormVisible = false
      this.getDataList()
    },
    // 删除流程定义文件
    handleDelete2(data) {
      this.$confirm('此操作将永久删除【' + data.title + '】流程的所有信息，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        var id = data.id
        console.log(id)
        this.deleteRequest(`/process/processFile/${id}`).then((resp) => {
          console.log('-----------------------------删除部署文件--------------------------')
          console.log(resp)
          this.$message.success(resp.msg)
          this.getDataList()
          // this.getDataList()
        })
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    onSubmit() {
      console.log('submit!')
    }
  }
}
</script>

<style scoped>
.right1{
  height: 300px;
}
/* .right2{
  height: 240px;
}
.right3{
  height: 240px;
}
.demo-form-inline{
  width: 97%;
  display: flex;
  justify-content: space-between;
  margin: 20px auto;
  margin-bottom: 0px;
  margin-top: 5px;
}
.el-table{
  width: 97%;
  margin: 15px auto
}
.first{
  width: 97%;
  margin: 0px auto
}
.el-dialog__body{
  height: 150px !important;
  overflow: auto;
} */
@import '../../assets/css/top-form.css';
@import '../../assets/css/table.css';
.tabs-content {
  width: 100%;
  height: 78vh !important;
}
/** 分页器 start  */
.block {
  position: absolute;
  top: 94% !important;
  right: 1.5%;
}
.el-pagination {
  align-items: center;
}
.el-pagination /deep/ span:not([class*=suffix]) {
  font: 1vw '宋体' !important;
  padding-top: 0px;
}
.el-pagination /deep/ .el-pagination__total {
  padding-top: 5px !important;
}
.el-pagination /deep/ .el-input__inner {
  font: 1vw '宋体' !important;
}
/** 分页器 end */
</style>
